home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / oleo-1_4.lha / oleo-1.4 / ir.c < prev    next >
C/C++ Source or Header  |  1993-03-20  |  5KB  |  261 lines

  1. /*    Copyright (C) 1992, 1993 Free Software Foundation, Inc.
  2.  
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2, or (at your option)
  6. any later version.
  7.  
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11. GNU General Public License for more details.
  12.  
  13. You should have received a copy of the GNU General Public License
  14. along with this software; see the file COPYING.  If not, write to
  15. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  16.  
  17.  
  18. #include "ir.h"
  19.  
  20. #ifdef __STDC__
  21. int
  22. xx_IRencloses (xx_IntRectangle r1, xx_IntRectangle r2)
  23. #else
  24. int
  25. xx_IRencloses (r1, r2)
  26.      xx_IntRectangle r1;
  27.      xx_IntRectangle r2;
  28. #endif
  29. {
  30.   return (xx_IRhits_point (r1, xx_IRllx (r2), xx_IRlly (r2))
  31.       && (!xx_IRw (r2) || !xx_IRh (r2)
  32.           || (xx_IRhits_point (r1, xx_IRurx (r2), xx_IRlly (r2))
  33.           && xx_IRhits_point (r1, xx_IRllx (r2), xx_IRury (r2))
  34.           && xx_IRhits_point (r1, xx_IRurx (r2), xx_IRury (r2)))));
  35. }
  36.  
  37. #ifdef __STDC__
  38. int
  39. xx_IRencloses_width (xx_IntRectangle r1, xx_IntRectangle r2)
  40. #else
  41. int
  42. xx_IRencloses_width (r1, r2)
  43.      xx_IntRectangle r1;
  44.      xx_IntRectangle r2;
  45. #endif
  46. {
  47.   return (xx_IRhits_point (r1, xx_IRllx (r2), xx_IRlly (r1))
  48.       && (!xx_IRw (r2)
  49.           || (xx_IRhits_point (r1, xx_IRurx (r2), xx_IRlly (r1)))));
  50. }
  51.  
  52.  
  53. #ifdef __STDC__
  54. int
  55. xx_IRequiv (xx_IntRectangle r1, xx_IntRectangle r2)
  56. #else
  57. int
  58. xx_IRequiv (r1, r2)
  59.      xx_IntRectangle r1;
  60.      xx_IntRectangle r2;
  61. #endif
  62. {
  63.   return (xx_IRllx (r2) == xx_IRllx (r1) &&
  64.       xx_IRlly (r2) == xx_IRlly (r1) &&
  65.       xx_IRurx (r2) == xx_IRurx (r1) &&
  66.       xx_IRury (r2) == xx_IRury (r1));
  67. }
  68.  
  69.  
  70. #define MAX(A,B)    ((A) > (B) ? (A) : (B))
  71. #define MIN(A,B)    ((A) < (B) ? (A) : (B))
  72. #define ROUND(F)     (int)((F) + 0.5)
  73.  
  74. #ifdef __STDC__
  75. void
  76. xx_IRbound (xx_IntRectangle r1, xx_IntRectangle r2)
  77. #else
  78. void
  79. xx_IRbound (r1, r2)
  80.      xx_IntRectangle r1;
  81.      xx_IntRectangle r2;
  82. #endif
  83. {
  84.   int x, y;
  85.   int X, Y;
  86.  
  87.   x = MIN (xx_IRllx (r1), xx_IRllx (r2));
  88.   y = MIN (xx_IRlly (r1), xx_IRlly (r2));
  89.   X = MAX (xx_IRurx (r1), xx_IRurx (r2));
  90.   Y = MAX (xx_IRury (r1), xx_IRury (r2));
  91.  
  92.   r1->x = x;
  93.   r1->y = y;
  94.   r1->w = X - x + 1;
  95.   r1->h = Y - y + 1;
  96. }
  97.  
  98.  
  99. #ifdef __STDC__
  100. int
  101. xx_IRarea (xx_IntRectangle r)
  102. #else
  103. int
  104. xx_IRarea (r)
  105.      xx_IntRectangle r;
  106. #endif
  107. {
  108.   return r->w * r->h;
  109. }
  110.  
  111.  
  112. #ifdef __STDC__
  113. int
  114. xx_IRhits_point (xx_IntRectangle rect, int x, int y)
  115. #else
  116. int
  117. xx_IRhits_point (rect, x, y)
  118.      xx_IntRectangle rect;
  119.      int x;
  120.      int y;
  121. #endif
  122. {
  123.   return (x >= rect->x
  124.       && y >= rect->y
  125.       && x < rect->x + rect->w
  126.       && y < rect->y + rect->h);
  127. }
  128.  
  129.  
  130. #ifdef __STDC__
  131. void
  132. xx_IRclip (xx_IntRectangle r1, xx_IntRectangle r2)
  133. #else
  134. void
  135. xx_IRclip (r1, r2)
  136.      xx_IntRectangle r1;
  137.      xx_IntRectangle r2;
  138. #endif
  139. {
  140.   int x = MAX (r1->x, r2->x);
  141.   int y = MAX (r1->y, r2->y);
  142.   int X = MIN (xx_IRurx (r1), xx_IRurx (r2));
  143.   int Y = MIN (xx_IRury (r1), xx_IRury (r2));
  144.   int t;
  145.   r1->x = x;
  146.   r1->y = y;
  147.   t = X - x + 1;
  148.   if (t < 0)
  149.     r1->w = 0;
  150.   else
  151.     r1->w = t;
  152.   t = Y - y + 1;
  153.   if (t < 0)
  154.     r1->h = 0;
  155.   else
  156.     r1->h = t;
  157. }
  158.  
  159.  
  160.  
  161.  
  162. #ifdef __STDC__
  163. int
  164. xx_IRsubtract (xx_IntRectangle outv, xx_IntRectangle a, xx_IntRectangle b)
  165. #else
  166. int
  167. xx_IRsubtract (outv, a, b)
  168.      xx_IntRectangle outv;
  169.      xx_IntRectangle a;
  170.      xx_IntRectangle b;
  171. #endif
  172. {
  173.   struct xx_sIntRectangle arect;
  174.   struct xx_sIntRectangle brect;
  175.   int outp = 0;
  176.  
  177.   if (!(a->w && a->h))
  178.     return 0;
  179.   if (!(b->w && b->h))
  180.     {
  181.       *outv = *a;
  182.       return 1;
  183.     }
  184.  
  185.   arect = *a;
  186.   brect = *b;
  187.  
  188.   xx_IRclip (&brect, &arect);
  189.  
  190.   if (xx_IRhits_point (&arect,
  191.                xx_IRllx (&brect),
  192.                xx_IRlly (&brect)))
  193.     {
  194.       /*
  195.  
  196.      -----------------.
  197.     |        |//////brect
  198.     |        |////////|
  199.         |        |---------
  200.     |   I    |  II    |
  201.     |      |        |
  202.          -----------------  arect
  203.     */
  204.  
  205.       /* I  */
  206.       outv[outp] = arect;
  207.       outv[outp].w = xx_IRllx (&brect) - xx_IRllx (&arect);
  208.  
  209.       if (outv[outp].w)
  210.     ++outp;
  211.  
  212.       /* II */
  213.       outv[outp] = arect;
  214.       outv[outp].x = xx_IRllx (&brect);
  215.       outv[outp].w = xx_IRurx (&arect) - xx_IRllx (&brect) + 1;
  216.       outv[outp].h = xx_IRlly (&brect) - xx_IRlly (&arect);
  217.  
  218.       if (outv[outp].h)
  219.     ++outp;
  220.  
  221.       arect.w = xx_IRurx (&arect) - xx_IRllx (&brect) + 1;
  222.       arect.h = xx_IRury (&arect) - xx_IRlly (&brect) + 1;
  223.       arect.x = xx_IRllx (&brect);
  224.       arect.y = xx_IRlly (&brect);
  225.     }
  226.  
  227.   if (xx_IRhits_point (&arect,
  228.                xx_IRurx (&brect),
  229.                xx_IRury (&brect)))
  230.     {
  231.       /*
  232.               --------------
  233.                  |           I  | arect
  234.                  |--------------|
  235.                  |////////|     |
  236.                  |////////|  II |
  237.                   --------------
  238.              brect
  239.  
  240.  
  241.     */
  242.  
  243.       /* I */
  244.       outv[outp] = arect;
  245.       outv[outp].h = xx_IRury (&arect) - xx_IRury (&brect);
  246.       outv[outp].y = xx_IRury (&brect) + 1;
  247.       if (outv[outp].h)
  248.     ++outp;
  249.  
  250.       /* II */
  251.       outv[outp] = arect;
  252.       outv[outp].h = xx_IRury (&brect) - xx_IRlly (&arect) + 1;
  253.       outv[outp].w = xx_IRurx (&arect) - xx_IRurx (&brect);
  254.       outv[outp].x = xx_IRurx (&brect) + 1;
  255.  
  256.       if (outv[outp].w)
  257.     ++outp;
  258.     }
  259.   return outp;
  260. }
  261.